package cn.easy.jacob.service;

import cn.easy.jacob.rocketmq.RocketMQClient;
import org.apache.commons.io.FileUtils;
import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.remoting.exception.RemotingException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.File;

@Component
public class RocketMQService {
    @Autowired
    RocketMQClient rocketMQClient;
    @Autowired
    private JacobService jacobService;

    public void sendMessage(MessageExt messageExt) throws Exception {
        File file = jacobService.fileDispatch(messageExt);
        Message message = new Message("jacob-message", messageExt.getTags(), messageExt.getKeys(), FileUtils.readFileToByteArray(file));
        try {
            rocketMQClient.sendMessage(message);
            file.delete();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (RemotingException e) {
            e.printStackTrace();
        } catch (MQClientException e) {
            e.printStackTrace();
        } catch (MQBrokerException e) {
            e.printStackTrace();
        }
    }
}
